Habitats dataset

Column

All habitat data were intersected with Exclusive Economical Zone layer to calculate the area in km^2, then were divided in coastal habitats and oceanic habitats.

Column

Richnes values are totals per each zone: Oceanic and Coastal, same for Shannon H: use filter or search to show different Countries and the next button below to scroll down the rows.

Habitats charts

Column

Countries by habitat richness

Column

Countries by habitat diversity

All habitats

Column

Total habitat richness

Coastal habitats richness

Column

Coastal habitat richness

Oceanic habitats richness

Column

Oceanic habitat richness

Coastal habitats diversity

Column

Coastal habitats diversity calculated with Shannon index (H)

Oceanic habitats diversity

Column

Oceanic habitats diversity calculated with Shannon index (H)

---
title: "Dashboard_test"
output: 
  flexdashboard::flex_dashboard:
    vertical_layout: scroll
    theme: spacelab
    source_code: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
```

```{r Loading libraries}
library(tidyverse)
library(stringr)
library(mapview)
library(maptools)
library(DT)
library(vegan)
library(plotly)

world<- rgdal::readOGR("datasources/world_eez_with_land/EEZ_land_v2_201410.shp", verbose = FALSE)
habitats <- readRDS("datasources/habitats.RDS")

```

Habitats dataset
=======================================================================

Column {data-height=700}
-----------------------------------------------------------------------

### All habitat data were intersected with Exclusive Economical Zone layer to calculate the area in ```km^2```, then were divided in coastal habitats and oceanic habitats. 
```{r}
datatable(
  habitats %>%
    select(Country, Zone, Habitat, Area),
  filter = 'top',
  rownames = FALSE
) %>%
  formatRound("Area", digits = 1)
```

Column {data-height=700}
-----------------------------------------------------------------------

### Richnes values are totals per each zone: Oceanic and Coastal, same for Shannon H: use filter or search to show different Countries and the next button below to scroll down the rows.
```{r Datatable diversity}
habitats %>%
  add_count(Country, Zone) %>%
  group_by(Country, Zone) %>%
  mutate(H = diversity(Area, index = "shannon", 2))  %>%
  select(Country, Zone, Richness = n, Shannon_H = H) %>%
  unique(.) -> rich

datatable(rich,
          filter = 'top',
          rownames = FALSE) %>%
  formatRound("Shannon_H", digits = 1)
```


Habitats charts
=======================================================================

Column {data-height=700}
-----------------------------------------------------------------------

### Countries by habitat richness

```{r fig.height=25}
habitats %>%
  group_by(Country) %>%
  tally() %>%
  select(Country, Richness = n) -> rich

p1 <- rich %>%
  ungroup() %>%
  arrange(-Richness) %>%
  #slice(., 1:20) %>%
  ggplot(aes(x = reorder(Country, Richness), y = Richness)) +
  geom_point(stat = 'identity', fill = "black", size = 4) +   # Draw points
  geom_segment(aes(
    x = Country,
    xend = Country,
    y = 0,
    yend = Richness
  ),
  color = "black") +   # Draw dashed lines
  labs(
    title = "Habitat richness",
    x = "",
    y = "Richness (N° of habitats)",
    caption = "source: hdc"
  ) +
  coord_flip()
ggplotly(p1)

```


Column {data-height=700}
-----------------------------------------------------------------------

### Countries by habitat diversity
```{r fig.height=25}
habitats %>%
  group_by(Country) %>%
  summarise(H = diversity(Area, index = "shannon", 2)) -> rich

p1 <- rich %>%
  ungroup() %>%
  arrange(-H) %>%
  #slice(., 1:20) %>%
  ggplot(aes(x = reorder(Country, H), y = H)) +
  geom_point(stat = 'identity', fill = "black", size = 4) +   # Draw points
  geom_segment(aes(
    x = Country,
    xend = Country,
    y = 0,
    yend = H
  ),
  color = "black") +   # Draw dashed lines
  labs(
    title = "Habitat diversity",
    x = "",
    y = "Shannon H",
    caption = "source: hdc"
  ) +
  coord_flip()
ggplotly(p1)

```


All habitats
=======================================================================

Column {data-height=700}
-----------------------------------------------------------------------


### Total habitat richness

```{r Habitat richness, fig.height=10}
# habitat richness / diversity per country
habitats %>%
  group_by(Country, Zone) %>%
  tally() %>%
  group_by(Country) %>%
  mutate(tot.rich = sum(n)) -> rich

rich_map <- merge(world, filter(rich, Zone == "Coastal"), by = "Country")

mapview(rich_map, zcol = "tot.rich")

```

Coastal habitats richness
=======================================================================

Column {data-height=700}
-----------------------------------------------------------------------

### Coastal habitat richness

```{r Coastal habitat richness, fig.height=10}
# habitat richness / diversity per country
habitats %>%
  group_by(Country, Zone) %>%
  tally() %>%
  group_by(Country) %>%
  mutate(tot.rich = sum(n)) -> rich


rich_map <- merge(world, filter(rich, Zone == "Coastal"), by = "Country")

mapview(rich_map, zcol = "n")
```

Oceanic habitats richness
=======================================================================

Column {data-height=700}
-----------------------------------------------------------------------

### Oceanic habitat richness

```{r Oceanic habitat richness, fig.height=10}
# habitat richness per country
habitats %>%
  group_by(Country, Zone) %>%
  tally() %>%
  group_by(Country) %>%
  mutate(tot.rich = sum(n)) -> rich


rich_map <- merge(world, filter(rich, Zone == "Oceanic"), by = "Country")


mapview(rich_map, zcol = "n")
```



Coastal habitats diversity
=======================================================================

Column {data-height=700}
-----------------------------------------------------------------------
### Coastal habitats diversity calculated with Shannon index (H)

```{r Coastal habitat diversity, fig.height=10}
# habitat diversity per country

habitats %>%
  group_by(Country, Zone) %>%
  summarise(H = diversity(Area, index = "shannon", 2)) -> rich

div_map <- merge(world, filter(rich, Zone == "Coastal"), by = "Country")

mapview(div_map, zcol = "H")

```


Oceanic habitats diversity
=======================================================================

Column {data-height=700}
-----------------------------------------------------------------------
### Oceanic habitats diversity calculated with Shannon index (H)

```{r Oceanic habitat diversity, fig.height=10}
# habitat diversity per country
habitats %>%
  group_by(Country, Zone) %>%
  summarise(H = diversity(Area, index = "shannon", 2)) -> rich

div_map <- merge(world, filter(rich, Zone == "Oceanic"), by = "Country")
library(mapview)

mapview(div_map, zcol = "H")

```